package com.xjoyt.pay.master.services;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xjoyt.pay.abstracts.AbstractService;
import com.xjoyt.pay.commons.ObjectUtils;
import com.xjoyt.pay.commons.PayTypeUtils;
import com.xjoyt.pay.dto.ExecuteResult;
import com.xjoyt.pay.dto.PageParam;
import com.xjoyt.pay.dto.PageResult;
import com.xjoyt.pay.master.params.CompanyPageParam;
import com.xjoyt.pay.master.po.Company;
import com.xjoyt.pay.master.po.CompanyRate;
import com.xjoyt.pay.master.repository.CompanyRateRepository;
import com.xjoyt.pay.master.repository.CompanyRepository;
import com.xjoyt.pay.master.vo.CompanyRateMap;
import com.xjoyt.pay.master.vo.RateEntry;
import com.xjoyt.pay.vo.PayType;

@Service
public class CompanyRateService extends AbstractService {

	@Autowired
	private CompanyRateRepository repository;

	@Autowired
	private CompanyRepository companyRepository;

	public List<CompanyRate> findAllByCompanyId(String companyId) {
		return repository.findAllByCompanyId(companyId);
	}

	public ExecuteResult<CompanyRate> save(CompanyRate entity) {
		entity = repository.save(entity);
		return ExecuteResult.ok(entity);
	}

	public ExecuteResult<?> delete(String id) {
		repository.delete(id);
		return ExecuteResult.ok();
	}

	public PageResult<CompanyRateMap> loadPageRate(PageParam<CompanyPageParam> param) {
		PageResult<Company> page = companyRepository.loadPage(param);
		List<CompanyRateMap> rows = new ArrayList<>();

		List<String> ids = new ArrayList<>();
		page.forEach(item -> {
			ids.add(item.id);
			CompanyRateMap vo = new CompanyRateMap();
			rows.add(vo);
			ObjectUtils.fieldCopy(item, vo);
		});

		List<CompanyRate> list = CollectionUtils.isEmpty(ids) ? new ArrayList<>()
				: repository.findAllByCompanyIdIn(ids);
		rows.forEach(item -> {
			item.rateList = new ArrayList<>();
			for (PayType type : PayType.values()) {
				RateEntry entry = new RateEntry();
				entry.type = type;
				item.rateList.add(entry);
				entry.name = PayTypeUtils.getName(type);
				for (CompanyRate companyRate : list) {
					if (type.equals(companyRate.type) && item.id.equals(companyRate.company.id)) {
						entry.rate = companyRate.rate;
						break;
					}
				}
			}
		});

		return new PageResult<>(page.getTotal(), rows);
	}

	public ExecuteResult<?> saveRate(CompanyRateMap param) {
		Company company = companyRepository.findOne(param.id);
		if (company == null) {
			return ExecuteResult.fail("数据不存在");
		}
		repository.findAllByCompanyId(param.id).forEach(item -> repository.delete(item));
		if (CollectionUtils.isEmpty(param.rateList)) {
			return ExecuteResult.ok();
		}

		for (RateEntry item : param.rateList) {
			if (StringUtils.isBlank(PayTypeUtils.getName(item.type)))
				continue;
			CompanyRate entity = new CompanyRate();
			entity.type = item.type;
			entity.rate = item.rate;
			entity.company = company;
			repository.save(entity);
		}

		return ExecuteResult.ok();
	}
}
